package com.sun.module_mine.ui.timer

import androidx.compose.animation.AnimatedContent
import androidx.compose.animation.ContentTransform
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.animation.SizeTransform
import androidx.compose.animation.core.tween
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.slideInVertically
import androidx.compose.animation.slideOutVertically
import androidx.compose.animation.togetherWith
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.sun.module_base.widget.Material3Card

/**
 *@author:sunc
 *@time :2025/8/14 15:40
 *@desc: 描述
 */
enum class TimerAnimation {
    NONE, SLIDE_TOP, SLID_BOTTOM, BOUNCE_TOP, BOUNCE_BOTTOM;
}

@Composable
fun TimerViewAnimation(
    timerData: TimerData,
    modifier: Modifier= Modifier,
    textStyle: TextStyle = TextStyle.Default.copy(fontSize = MaterialTheme.typography.bodyMedium.fontSize),
    textColor: Color = Color.Black,
    fontFamily: FontFamily = FontFamily.Default,
    fontWeight: FontWeight = FontWeight.SemiBold,
    backgroundPadding: Dp = 0.dp,
    timeAnimation: TimerAnimation = TimerAnimation.SLIDE_TOP,
    timeDurationSlide: Int = 500,
    timeSpace: String = " : ",
    icon: ImageVector? = null,
    iconColor: ColorFilter? = null
) {
    Material3Card(modifier = modifier) {
        Row(
            modifier =
                Modifier
                    .padding(start = 8.dp, end = 8.dp)
                    .padding(all = backgroundPadding)
                    .alpha(0f),
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.Center
        ) {
            icon?.let {
                Image(
                    modifier = Modifier.padding(end = 4.dp),
                    imageVector = it,
                    colorFilter = iconColor ?: ColorFilter.tint(textColor),
                    contentDescription = null
                )
            }
            Text(
                text = "00${timeSpace}00${timeSpace}00",
                style = textStyle,
                textAlign = TextAlign.Center,
                color = textColor,
                fontFamily = fontFamily,
                fontWeight = fontWeight
            )
        }


        Row(
            modifier = Modifier
                .padding(start = 8.dp, end = 8.dp)
                .padding(all = backgroundPadding),
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.Center
        ) {
            icon?.let {
                Image(
                    modifier = Modifier.padding(end = 4.dp),
                    imageVector = it,
                    colorFilter = iconColor ?: ColorFilter.tint(textColor),
                    contentDescription = null
                )
            }
            TimeViewAnimationContent(
                timeContent = timerData.hourTens,
                textStyle = textStyle,
                timerAnimation = timeAnimation,
                timerDurationSlide = timeDurationSlide,
                textColor = textColor,
                fontFamily = fontFamily,
                fontWeight = fontWeight
            )

            TimeViewAnimationContent(
                timeContent = timerData.hourZero,
                textStyle = textStyle,
                timerAnimation = timeAnimation,
                timerDurationSlide = timeDurationSlide,
                textColor = textColor,
                fontFamily = fontFamily,
                fontWeight = fontWeight
            )

            TimeViewSeparator(
                timeSpace = timeSpace,
                textStyle = textStyle,
                textColor = textColor,
                fontFamily = fontFamily,
                fontWeight = fontWeight
            )

            TimeViewAnimationContent(
                timeContent = timerData.minuteTens,
                textStyle = textStyle,
                timerAnimation = timeAnimation,
                timerDurationSlide = timeDurationSlide,
                textColor = textColor,
                fontFamily = fontFamily,
                fontWeight = fontWeight
            )

            TimeViewAnimationContent(
                timeContent = timerData.minuteZero,
                textStyle = textStyle,
                timerAnimation = timeAnimation,
                timerDurationSlide = timeDurationSlide,
                textColor = textColor,
                fontFamily = fontFamily,
                fontWeight = fontWeight
            )

            TimeViewSeparator(
                timeSpace = timeSpace,
                textStyle = textStyle,
                textColor = textColor,
                fontFamily = fontFamily,
                fontWeight = fontWeight
            )

            TimeViewAnimationContent(
                timeContent = timerData.secondTens,
                textStyle = textStyle,
                timerAnimation = timeAnimation,
                timerDurationSlide = timeDurationSlide,
                textColor = textColor,
                fontFamily = fontFamily,
                fontWeight = fontWeight
            )

            TimeViewAnimationContent(
                timeContent = timerData.secondZero,
                textStyle = textStyle,
                timerAnimation = timeAnimation,
                timerDurationSlide = timeDurationSlide,
                textColor = textColor,
                fontFamily = fontFamily,
                fontWeight = fontWeight
            )
        }
    }

}

@Composable
fun TimeViewAnimationContent(
    timeContent: String,
    textStyle: TextStyle,
    textColor: Color,
    timerAnimation: TimerAnimation,
    timerDurationSlide: Int,
    fontFamily: FontFamily,
    fontWeight: FontWeight
) {
    AnimatedContent(targetState = timeContent, transitionSpec = {
        addAnimation(
            timerDurationSlide = timerDurationSlide,
            timerAnimation = timerAnimation
        ).using(
            sizeTransform = SizeTransform(clip = true)
        )
    }) { targetCount ->
        Text(
            text = targetCount,
            style = textStyle,
            textAlign = TextAlign.Center,
            color = textColor,
            fontFamily = fontFamily,
            fontWeight = fontWeight
        )
    }
}

@OptIn(ExperimentalAnimationApi::class)
private fun addAnimation(
    timerDurationSlide: Int,
    timerAnimation: TimerAnimation
): ContentTransform {
    val duration = when (timerAnimation) {
        TimerAnimation.NONE -> 0
        else -> timerDurationSlide
    }
    return slideInVertically(animationSpec = tween(durationMillis = duration)) { height ->
        when (timerAnimation) {
            TimerAnimation.NONE -> 0
            TimerAnimation.SLIDE_TOP -> height
            TimerAnimation.SLID_BOTTOM -> height
            TimerAnimation.BOUNCE_TOP -> height
            TimerAnimation.BOUNCE_BOTTOM -> height
        }
    } + fadeIn(animationSpec = tween(durationMillis = duration)) togetherWith
            slideOutVertically(
                animationSpec = tween(durationMillis = duration)
            ) { height ->
                when (timerAnimation) {
                    TimerAnimation.NONE -> 0
                    TimerAnimation.SLIDE_TOP -> height
                    TimerAnimation.SLID_BOTTOM -> height
                    TimerAnimation.BOUNCE_TOP -> height
                    TimerAnimation.BOUNCE_BOTTOM -> height
                }
            } + fadeOut(animationSpec = tween(durationMillis = duration))

}

@Composable
fun TimeViewSeparator(
    timeSpace: String,
    textStyle: TextStyle,
    textColor: Color,
    fontFamily: FontFamily,
    fontWeight: FontWeight
) {
    Text(
        text = timeSpace,
        style = textStyle,
        textAlign = TextAlign.Center,
        color = textColor,
        fontFamily = fontFamily,
        fontWeight = fontWeight
    )
}
